#!/usr/bin python3
# -*- coding:UTF-8 -*-
# Author: nigo
import plotly.graph_objects as go
import pandas as pd
import json

def get_geojson():
    """获取深圳市geojson"""
    with open('深圳市.json') as f:
        text = f.read()
    return json.loads(text)

if __name__ == "__main__":
    # 数据 （随书附件Excel复制后运行）
    df = pd.read_clipboard(delimiter='\s+',thousands=',')
    # 读取geojson数据
    city = get_geojson()
    # 轨迹
    map = go.Choroplethmapbox(geojson=city, locations=df['街道'],z=df['交通指数'],
                             featureidkey='properties.NAME',colorscale='Viridis',
                              marker_opacity=0.5,marker_line_width=0,colorbar_title='交通指数')
    # 画布
    fig = go.Figure(map)
    # 更新样式
    fig.update_layout(mapbox_style="carto-positron",
                      mapbox_zoom=9,title = '深圳交通指数',
                      mapbox_center = {"lat": 22.555341, "lon": 114.123885})
    # 更新样式 设置画布边界
    fig.update_layout(margin={"r":0,"t":40,"l":0,"b":0})
    # 显示画布
    fig.show()
